package com.tanhua.dubbo.server.api;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveUsers(Long userId, Long friendId) {

        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return null;
        }
        // 检测是否该好友关系是否存在
        Query query = Query.query(Criteria
                .where("userId").is(userId)
                .and("friendId").is(friendId));
        long count = this.mongoTemplate.count(query, Users.class);
        if (count > 0) {
            return null;
        }
        //我注册好友的关系
        Users users = new Users();
        users.setId(ObjectId.get());
        users.setUserId(userId);
        users.setFriendId(friendId);
        users.setDate(System.currentTimeMillis());
        this.mongoTemplate.save(users);
        //h好友注册我的关系
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);
        this.mongoTemplate.save(users);
        return users.getId().toHexString();
    }

    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        Query query=Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        //我删除好友信息
        DeleteResult remove = this.mongoTemplate.remove(query, Users.class);

        Query query1=Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId));
        //好友删除我的信息
        DeleteResult remove1 = this.mongoTemplate.remove(query1, Users.class);
        return remove.getDeletedCount()>0&&remove1.getDeletedCount()>0;
    }

    @Override
    public List<Users> queryAllUsersList(Long userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return null;
        }
        Query query=Query.query(Criteria.where("userId").is(userId));
        List<Users> users = this.mongoTemplate.find(query, Users.class);
        if (CollUtil.isEmpty(users)) {
            return null;
        }
        return users;
    }

    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageInfo<Users> pageInfo=new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        if (!ObjectUtil.isAllNotEmpty(userId,page,pageSize)) {
            return pageInfo;
        }

        Pageable pageable= PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("created")));
        Query query=Query.query(Criteria.where("userId").is(userId)).with(pageable);
        List<Users> users = this.mongoTemplate.find(query, Users.class);
        if (CollUtil.isEmpty(users)) {
            return null;
        }
        pageInfo.setRecords(users);
        return pageInfo;
    }
}
